const postcss = require('postcss')
const valueParser = require('postcss-value-parser')

module.exports = postcss.plugin('rewrite-vw-wh', (options) => {
    return (root) => {
        root.walkDecls((decl) => {
            const parsed = valueParser(decl.value)
            parsed.walk((node) => {
                if (node.type === 'word' && node.value.match(/vw|vh|vmin|vmax/)) {
                    node.value = node.value.replace(/(\+|\-)?(\d*\.)?\d+(vw|vh|vmin|vmax)/g, function (match) {
                        return `calc(${match} / var(--zoom))`
                    })
                }
            })
            decl.value = parsed.toString()
        })
    }
})
